<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="24" :xs="24">
        <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
          <el-form-item label="部门名称" prop="deptName">
            <el-input v-model="queryParams.deptName" placeholder="请输入部门名称" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="部门编码" prop="deptCode">
            <el-input v-model="queryParams.deptCode" placeholder="请输入部门编码" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item label="部门领导" prop="deptLeader">
            <el-input v-model="queryParams.deptLeader" placeholder="请输入部门领导" size="small" clearable style="width: 150px"/>
          </el-form-item>
          <el-form-item>
            <el-button type="primary"  size="mini" icon="el-icon-search" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery('queryForm')">重置</el-button>
          </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" v-hasPermi="['saveSystemDept']"  @click="handleAdd">新增一级部门</el-button>
          </el-col>

          <right-toolbar :showSearch.sync="showSearch" @queryTable="querySystemDeptTreeResList" :columns="columns"></right-toolbar>
        </el-row>

        <el-table :stripe="true" v-loading="loading" :data="dataList" row-key="deptId" :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
          <el-table-column  prop="deptName" label="部门名称" width="300" />
          <el-table-column prop="deptCode" label="部门编码" width="200"/>
          <el-table-column prop="deptLeader" label="部门领导"  width="150"/>
          <el-table-column prop="deptTelephone" label="部门电话" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="deptFax" label="部门传真" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column prop="remark" label="备注" :show-overflow-tooltip="true"></el-table-column>
          <el-table-column label="内置部门" prop="builtInName" align="center" width="150">
            <template slot-scope="scope">
              <span :style="'color:'+scope.row.builtInColor">{{scope.row.builtInName}}</span>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200" class-name="small-padding fixed-width">
            <template slot-scope="scope">
              <el-button size="mini" type="text" icon="el-icon-plus" v-hasPermi="['saveSystemDept']"  @click="handleAdd(scope.row)">新增</el-button>
              <el-button size="mini" type="text" icon="el-icon-edit" v-hasPermi="['updateSystemDept']"  @click="handleUpdate(scope.row)">修改</el-button>
              <el-button size="mini" v-if="scope.row.builtIn==='NO'"  type="text" icon="el-icon-delete" v-hasPermi="['deleteSystemDept']"  @click="handleDelete(scope.row)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
    </el-row>

    <!-- 添加或修改参数配置对话框 -->
    <el-dialog :title="title" :visible.sync="addUpdateOpen" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="addUpdateOpenRules"  label-width="80px">
        <el-form-item label="上级部门" prop="parentId">
          <treeselect v-model="form.parentId"
                      :options="deptOptions"
                      :defaultExpandLevel="10"
                      :normalizer="normalizerTree"
                      :show-count="true"
                      :hide-required-asterisk="true"
                      :disabled="(form.deptId === undefined && form.parentId !== -1) || (form.builtIn!=='NO')" autocomplete="off"
                      placeholder="请选择上级部门" />
        </el-form-item>

        <el-form-item label="部门名称" prop="deptName">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.deptName" placeholder="请输入部门名称" maxlength="200" />
        </el-form-item>

        <el-form-item label="部门领导" prop="deptLeader">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.deptLeader" placeholder="请输入部门领导" maxlength="20" />
        </el-form-item>

        <el-form-item label="部门电话" prop="deptTelephone">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.deptTelephone" placeholder="请输入部门电话" maxlength="50" />
        </el-form-item>

        <el-form-item label="部门传真" prop="deptFax">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.deptFax" placeholder="请输入部门传真" maxlength="50" />
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input :hide-required-asterisk="true" autocomplete="off" v-model="form.remark" placeholder="请输入备注" maxlength="100" />
        </el-form-item>

        <el-form-item label="排序" prop="sort">
          <el-input-number :hide-required-asterisk="true" v-model="form.sort" :min="1" :max="9999" label="排序" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button size="medium" type="primary" @click="submitForm">确 定</el-button>
        <el-button size="medium" @click="cancelAddUpdateOpen">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
  import {querySystemDeptTreeResList, queryNormalSelectSystemDeptTreeResList, saveSystemDept, updateSystemDept, deleteSystemDeptByDeptId} from '@/api/system/systemDept'
  import Treeselect from "@riophae/vue-treeselect";
  import "@riophae/vue-treeselect/dist/vue-treeselect.css";
  //默认查询参数
  let defaultQueryParams = {
    deptLeader: undefined,
    deptCode: undefined,
    remark: undefined,
    deptName: undefined
  }
  export default {
    name: "systemDeptAdmin",
    components: {Treeselect},
    data() {
      return {
        // 遮罩层
        loading: true,
        // 显示搜索条件
        showSearch: true,
        // table表格数据
        dataList: [],
        // 上级部门选择
        deptOptions: [],
        // 弹出层标题
        title: "",
        // 查询参数
        queryParams: { ...defaultQueryParams },
        //列信息
        columns: [],
        // 是否显示新增修改弹出层
        addUpdateOpen: false,
        // 增加修改表单参数
        form: {},
        // 新增修改表单校验
        addUpdateOpenRules: {
          deptName: [
            { required: true, message: "部门名称不能为空",trigger: "blur" }
          ],
          deptLeader: [
            { required: true, message: "部门领导不能为空",trigger: "blur" }
          ],
          sort: [
            { required: true, message: "排序不能为空",trigger: "blur" }
          ]
        }
      }
    },
    created() {
      this.querySystemDeptTreeResList();
    },
    methods: {
      /** 查询部门列表 */
      querySystemDeptTreeResList() {
        this.loading = true
        querySystemDeptTreeResList(this.queryParams).then(response => {
          this.dataList = response.data;
        }).finally(() =>{
          //关闭加载特效
          this.loading = false;
        });
      },
      /** 查询顶级部门列表 */
      queryNormalSelectSystemDeptTreeResList() {
        queryNormalSelectSystemDeptTreeResList({}).then(response => {
          this.deptOptions = [];
          const dept = { deptId: "-1", deptName: '一级部门', children: [] };
          dept.children = response.data;
          this.deptOptions.push(dept);
        });
      },
      /** 转换部门数据结构 */
      normalizerTree(node) {
        if (node.children && !node.children.length) {
          delete node.children;
        }
        return {
          id: node.deptId,
          label: node.deptName,
          children: node.children
        };
      },
      /** 新增按钮操作 */
      handleAdd(row) {
        this.reset();
        //加载顶级部门
        this.queryNormalSelectSystemDeptTreeResList();
        //修改表单填充
        if (row != null && row.deptId) {
          this.form.parentId = row.deptId;
        }else {
          this.form.parentId = "-1";
        }
        this.addUpdateOpen = true;
        this.title = "添加部门";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        //加载顶级部门
        this.queryNormalSelectSystemDeptTreeResList();
        //修改表单填充
        this.form = {
          deptId: row.deptId,
          parentId: row.parentId,
          deptName: row.deptName,
          deptLeader: row.deptLeader,
          deptTelephone: row.deptTelephone,
          deptFax: row.deptFax,
          sort: row.sort,
          remark: row.remark,
          builtIn: row.builtIn
        };
        this.addUpdateOpen = true;
        this.title = "修改部门";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        this.$confirm('是否确认删除所选部门?', "警告", {
          cancelButtonText: "取消",
          confirmButtonText: "确定",
          type: "warning"
        }).then(function() {
          const deptId={deptId:row.deptId}
          return deleteSystemDeptByDeptId(deptId);
        }).then(response => {
          this.$modal.msgSuccess(response.message)
          this.querySystemDeptTreeResList();
        }).catch(function() {});
      },
      /** 提交保存或修改按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.deptId !== undefined) {
              updateSystemDept(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.querySystemDeptTreeResList();
                this.addUpdateOpen = false
              });
            } else {
              saveSystemDept(this.form).then(response => {
                this.$modal.msgSuccess(response.message)
                this.querySystemDeptTreeResList();
                this.addUpdateOpen = false
              });
            }
          }
        });
      },
      // 取消修改增加弹出层按钮
      cancelAddUpdateOpen() {
        this.addUpdateOpen = false
      },
      /** 重置按钮操作 */
      resetQuery(fromId) {
        this.resetForm(fromId)
        this.handleQuery()
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.page = 1
        this.querySystemDeptTreeResList()
      },
      // 表单重置
      reset() {
        this.form = {
          deptName: undefined,
          deptId: undefined,
          deptLeader: undefined,
          deptTelephone: undefined,
          deptFax: undefined,
          remark: undefined,
          parentId: "-1",
          builtIn: undefined,
          sort: 1
        };
        this.resetForm('form')
      }
    }
  }
</script>
